//Init the jiathis configuration
var jiathis_config = {
		appkey:{
			"tsina":"123"
		},
		hideMore:false
	};
//set the content for sharing 
function setShare(event) {
	jiathis_config.url = event.data.url;
	jiathis_config.summary = event.data.summary;
	jiathis_config.title = event.data.title;
}

var jiaThisScript = null;
var cities = null;
var industries = null;
var maxCountOfCity = 5;//The max count of cities that will show on the right tab.
var maxCountOfInd = 4;//The max count of industries that will show on the top tab.
var selectCityIndex = 999; //The index of city selected, default is 999.
var selectIndIndex = 999; //The index of industry selected, default is 999.
var followMeetingId = '';

$(document).ready(function(){
	//load recommend meetings.
	var recUrl = $('#recUrl').val();
	$.ajax({
		  type: 'POST',
		  url: recUrl,
		  success: displayRec,
		  dataType: 'json'
		});
	
	//load recent meetings.
	var recentUrl = $('#recentUrl').val();
	$.ajax({
		  type: 'POST',
		  url: recentUrl,
		  success: displayRecent,
		  dataType: 'json',
		  data: 'meetingQueryParasVO.firstresultno=0&meetingQueryParasVO.resultspergroup=12'
		});
	
	//load industries
	var indUrl = $('#indUrl').val();
	$.ajax({
		  type: 'POST',
		  url: indUrl,
		  success: displayIndustriesTab,
		  dataType: 'json'
		});
	
	//load cities
	var cityUrl = $('#cityUrl').val();
	$.ajax({
		  type: 'POST',
		  url: cityUrl,
		  success: displayCitiesTab,
		  dataType: 'json'
		});
	initEvent();
});
/**
 * Display recommend meetings.
 * @param pageBean
 */
function displayRec(pageBean) {
	var result = pageBean.objectList;
	for (var i = 0; i < result.length; i++) {
		appendRec(result[i]);
	}
	
	//generate pager link
	var table = $('#rec_table');
	var trPager = $(document.createElement('tr'));
	var tdPager = $(document.createElement('td'));
	tdPager.addClass('pager');
	tdPager.attr('colspan', 2);
	
	var firstresultno = pageBean.firstresultno;
	var totalCount = pageBean.totalCount;
	var resultspergroup = pageBean.resultspergroup;
	//calc current page number
	var currentPage = parseInt(firstresultno / resultspergroup) + 1;
	//calc total number of pages
	var totalPages = 1;
	if (totalCount % resultspergroup == 0) {
		totalPages = parseInt(totalCount / resultspergroup);
	} else {
		totalPages = parseInt(totalCount / resultspergroup) + 1;
	}
	
	//calc the begin page number
	var startPage = currentPage - 2;
	while (startPage < 1) {
		startPage++;
	}
	
	//calc the end page number
	var endPage = (startPage + 4) < totalPages ? (startPage + 4) : totalPages;
	//because the css of span is float:right，need append page num from max to min.
	for (var i = endPage; i >= startPage; i--) {
		var spanPager = $(document.createElement('span'));
		if (i == currentPage) {
			spanPager.addClass('pager_s');
		} else {
			spanPager.addClass('pager');
			spanPager.bind('click', {firstresultno : (i - 1) * 8, resultspergroup : 8}, pagerOnclick);
		}
		spanPager.html(i);
		tdPager.append(spanPager);
	}
	
	trPager.append(tdPager);
	table.append(trPager);
	//import jiathis script
	loadJiaThis();
}

function appendRec(meeting) {
	var table = $('#rec_table');
	
	var trName = $(document.createElement('tr'));
	
	var tdName = $(document.createElement('td'));
	tdName.addClass('met_main_t');
	var divName = $(document.createElement('div'));
	divName.addClass('met_main_t');
	var aName = $(document.createElement('a'));
	aName.addClass('met_main_t');
	aName.html(meeting.name);
	aName.attr('title', meeting.name);
	aName.attr('href', '#' + meeting.meetingId);
	aName.bind('click', {meetingId : meeting.meetingId}, getMeetingDetail);
	divName.append(aName);
	tdName.append(divName);
	trName.append(tdName);
	
	var tdDate = $(document.createElement('td'));
	tdDate.html(meeting.startDateStr + ' ' + meeting.city.name);
	tdDate.addClass('met_main_l');
	trName.append(tdDate);
	
	table.append(trName);
	
	var trAbs = $(document.createElement('tr'));
	trAbs.addClass('met_main_a');
	
	var tdAbs = $(document.createElement('td'));
	tdAbs.addClass('met_main_a');
	var divAbs = $(document.createElement('div'));
	divAbs.html(meeting.abstracT);
	divAbs.addClass('met_main_a');
	tdAbs.append(divAbs);
	trAbs.append(tdAbs);
	
	//===Add Share Button start===
	var tdShare = $(document.createElement('td'));
	tdShare.addClass('met_main_s');
	
	var divShare = $(document.createElement('div'));
	divShare.attr('id', 'jiathis_style_32x32');
	divShare.attr('style', 'float: right;padding: 0px 0px 0px 0px;');
	//TODO: need to do substring on abstracT to meet the limitation of weibo
	divShare.bind('mouseover', {url : '', summary : meeting.abstracT, title : '#' + meeting.name + '#'}, setShare);
	
	var aShareSina = $(document.createElement('a'));
	aShareSina.addClass('jiathis_button_tsina');
	divShare.append(aShareSina);
	
	var aShareQQ = $(document.createElement('a'));
	aShareQQ.addClass('jiathis_button_tqq');
	divShare.append(aShareQQ);
	
	tdShare.append(divShare);
	var spanShare = $(document.createElement('span'));
	spanShare.html('分享&nbsp;');
	spanShare.attr('style','float: right;');
	tdShare.append(spanShare);
	//===Add Share Button end===
	
	trAbs.append(tdShare);
	
	table.append(trAbs);

	var trFol = $(document.createElement('tr'));
	trFol.addClass('met_main_f');
	
	var tdFol = $(document.createElement('td'));
	tdFol.addClass('met_main_f');
	tdFol.attr('colspan','2');
	//tdFol.html('<span class="met_main_f_i">收藏&nbsp;</span><span style="float:right;">收藏人数 ' + (meeting.followCount == null ? '0' : meeting.followCount) + '&nbsp;</span>');
	var span_do_follow = $(document.createElement('span'));
	span_do_follow.addClass('met_main_f_i');
	span_do_follow.html('收藏&nbsp;');
	span_do_follow.bind('click', {meetingId : meeting.meetingId}, followMeeting);
	tdFol.append(span_do_follow);
	
	var span_follow_count = $(document.createElement('span'));
	span_follow_count.attr('style','float:right;');
	span_follow_count.html('收藏人数 ' + (meeting.followCount == null ? '0' : meeting.followCount) + '&nbsp;');
	span_follow_count.attr('id', 'span_fc_' + meeting.meetingId);
	tdFol.append(span_follow_count);
	
	trFol.append(tdFol);
	table.append(trFol);
}

/**
 * user follow the meeting.
 * @param event
 */
function followMeeting(event) {
	var followUrl = $('#followMetUrl').val();
	followMeetingId = event.data.meetingId;
	$.ajax({
		  type: 'POST',
		  url: followUrl,
		  success: afterFollowMeeting,
		  data : 'meetingVO.meetingId=' + followMeetingId,
		  dataType: 'json'
		});
}

function afterFollowMeeting(result) {
	if (result == '-2') {
		openLogin();
	} else if (result == '-1') {
		
	} else {
		$('#span_fc_' + followMeetingId).html('收藏人数 ' + result + '&nbsp;');
	}
}

/**
 * import jiathis script
 */
function loadJiaThis() {
	if (jiaThisScript != null) {
		jiaThisScript.remove();
	}
	jiaThisScript = $(document.createElement('script'));
	jiaThisScript.attr('type', 'text/javascript');
	jiaThisScript.attr('src', 'http://v2.jiathis.com/code/jia.js');
	jiaThisScript.attr('charset', 'utf-8');
	$(document).append(jiaThisScript);
}

function pagerOnclick(event) {
	var searchForm = $('#search_form');
	var postdata = searchForm.serialize() + '&meetingQueryParasVO.firstresultno=' + event.data.firstresultno + '&meetingQueryParasVO.resultspergroup=' + event.data.resultspergroup;
	$.ajax({
		  type: 'POST',
		  url: searchForm.attr('action'),
		  data: postdata,
		  success: freshRec,
		  dataType: 'json'
		});
}


/**
 * Display recent meetings.
 * @param pageBean
 */
function displayRecent(pageBean) {
	var result = pageBean.objectList;
	for (var i = 0; i < result.length; i++) {
		appendRecent(result[i], i + 1);
	}
}

function appendRecent(meeting, num) {
	var table = $('#recent_table');
	
	var trName = $(document.createElement('tr'));
	
	var tdNum = $(document.createElement('td'));
	tdNum.addClass('meet_l_1');
	tdNum.html(num + '.');
	trName.append(tdNum);
	
	var tdName = $(document.createElement('td'));
	tdName.addClass('meet_l_2');
	var divName = $(document.createElement('div'));
	divName.addClass('meet_l_2');
	var aName = $(document.createElement('a'));
	aName.html(meeting.name);
	aName.addClass('meet_l_2');
	aName.attr('title', meeting.name);
	aName.attr('href', '#' + meeting.meetingId);
	aName.bind('click', {meetingId : meeting.meetingId}, getMeetingDetail);
	divName.append(aName);
	tdName.append(divName);
	trName.append(tdName);
	
	table.append(trName);
	
	var trDate = $(document.createElement('tr'));
	trDate.addClass('meet_l_2');
	
	var tdDate = $(document.createElement('td'));
	tdDate.html(meeting.startDateStr + ' ' + meeting.city.name);
	tdDate.addClass('meet_l');
	tdDate.attr('colspan', '2');
	trDate.append(tdDate);
	
	table.append(trDate);
}

/**
 * Refresh recommend meetings.
 */
function freshRec(pageBean) {
	var divRight = $('#div_right');
	divRight.children().remove();
	
	var table = $(document.createElement('table'));
	table.attr('id', 'rec_table');
	table.attr('border', '0');
	table.attr('cellpadding', '0');
	table.attr('cellspacing', '0');
	table.addClass('met_main');
	divRight.append(table);
	
	$('#rec_table tr').remove();
	displayRec(pageBean);
}

/**
 * Load industries.
 * @param result
 */
function displayIndustriesTab(result) {
	selectIndIndex = 999;
	industries = result;
	var ul = $('#industries');
	var li = $(document.createElement('li'));
	li.addClass('toptab_all font_lb_yellow');
	li.html('全部');
	li.bind('click', {industryId : ''}, indTabOnclick);
	ul.append(li);
	for (var i = 0; i < (result.length > maxCountOfInd ? maxCountOfInd : result.length); i++) {
		appendIndustry(result[i], false);
	}
	if (result.length > maxCountOfInd) {
		li = $(document.createElement('li'));
		li.addClass('toptab_more font_lb_white');
		li.html('更多');
		li.attr('id', 'li_ind_more');
		li.hover(popupMoreInd, hideMoreInd);
		ul.append(li);
		
		var ul_ind_more_pop = $('#ul_ind_more_pop');
		$('#ul_ind_more_pop li').remove();
		for (var i = maxCountOfInd; i < result.length; i++ ) {
			var popli = $(document.createElement('li'));
			var popspan = $(document.createElement('span'));
			popspan.addClass('font_lb_black more_pop');
			popspan.html(result[i].name.substring(0, 2));
			popspan.attr('title', result[i].name);
			popli.append(popspan);
			popli.bind('click', {industryId : result[i].industryId}, indTabOnclick);
			ul_ind_more_pop.append(popli);
		}
	}
}
/**
 * popup more industries when click on more tab
 */
function popupMoreInd() {
		$('#div_ind_more_pop').show();
		$('#div_ind_more_pop').position(
			{
				of : $('#li_ind_more'),
				offset : '5 60'
		});
}

function hideMoreInd() {
	$('#div_ind_more_pop').hide();
}

function appendIndustry(industry, select) {
	var ul = $('#industries');
	var li = $(document.createElement('li'));
	//if (industry.name.length > 2) {
		var span = $(document.createElement('span'));
		span.html(industry.name.substring(0, 2));
		span.attr('title', industry.name);
		li.append(span);
	//} else {
	//	li.html(industry.name);
	//}
	if (select) {
		li.addClass('toptab_s font_lb_yellow');
	} else {
		li.addClass('toptab font_lb_white');
	}
	li.bind('click', {industryId : industry.industryId}, indTabOnclick);
	ul.append(li);
}

/**
 * Load cities.
 * @param result
 */
function displayCitiesTab(result) {
	selectCityIndex = 999;
	cities = result;
	var ul = $('#cities');
	var li = $(document.createElement('li'));
	li.addClass('rtab_all font_lb_yellow');
	li.html('<br/>全部');
	li.bind('click', {cityId : ''}, cityTabOnclick);
	ul.append(li);
	for (var i = 0; i < (result.length > maxCountOfCity ? maxCountOfCity : result.length); i++) {
		appendCity(result[i], false);
	}
	if (result.length > maxCountOfCity) {
		li = $(document.createElement('li'));
		li.addClass('rtab_more font_lb_white');
		li.html('<br/>更多');
		li.attr('id', 'li_city_more');
		li.hover(popupMoreCities, hideMoreCities);
		ul.append(li);
		
		var ul_city_more_pop = $('#ul_city_more_pop');
		$('#ul_city_more_pop li').remove();
		for (var i = maxCountOfCity; i < result.length; i++ ) {
			var popli = $(document.createElement('li'));
			var popspan = $(document.createElement('span'));
			popspan.addClass('font_lb_black more_pop');
			popspan.html(result[i].name.substring(0, 2));
			popspan.attr('title', result[i].name);
			popli.append(popspan);
			popli.bind('click', {cityId : result[i].cityId}, cityTabOnclick);
			ul_city_more_pop.append(popli);
		}
	}
}

/**
 * popup more cities when click on more tab
 */
function popupMoreCities() {
		$('#div_city_more_pop').show();
		$('#div_city_more_pop').position(
			{
				of : $('#li_city_more'),
				offset : '-20 70'
		});
}

function hideMoreCities() {
	$('#div_city_more_pop').hide();
}

function appendCity(city, select) {
	var ul = $('#cities');
	var li = $(document.createElement('li'));
	//if (city.name.length > 3) {
		var span = $(document.createElement('span'));
		span.html('<br/>' + city.name.substring(0, 2));
		span.attr('title', city.name);
		li.append(span);
	//} else {
	//	li.html('<br/>' + city.name);
	//}
	if (select) {
		li.addClass('rtab font_lb_yellow');
	} else {
		li.addClass('rtab font_lb_white');
	}
	li.bind('click', {cityId : city.cityId}, cityTabOnclick);
	ul.append(li);
}

/**
 * Initialize events of components.
 */
function initEvent() {
	$('#search_city').focusin(searchCityFocusin);
	//$('#search_date').focusin(searchDateFocusin);
	$('#search_ind').focusin(searchIndFocusin);
	$('#search_city').focusout(searchCityFocusout);
	//$('#search_date').focusout(searchDateFocusout);
	$('#search_ind').focusout(searchIndFocusout);
	$('#search_btn').bind('click', searchBtnOnclick);
	$('#div_ind_more_pop').hover(popupMoreInd, hideMoreInd);
	$('#div_city_more_pop').hover(popupMoreCities, hideMoreCities);
	var options = {
			changeMonth : true,
			changeYear : true,
			showMonthAfterYear : true,
			showButtonPanel : true,
			showOtherMonths : true,
			selectOtherMonths : true,
			dateFormat : 'yy-mm-dd',
			currentText: '今天',
			closeText : '关闭',
			dayNamesMin : ['日','一','二','三','四','五','六'],
			monthNames : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月'],
			monthNamesShort : ['1月','2月','3月','4月','5月','6月','7月','8月','9月','10月','11月','12月']
	};
	$('#search_date').datepicker(options);
	var dpFunc = $.datepicker._generateHTML; //record the original
	$.datepicker._generateHTML = function(inst){
		var thishtml = $( dpFunc.call($.datepicker, inst) ); //call the original
		
		thishtml = $('<div />').append(thishtml); //add a wrapper div for jQuery context
		
		//locate the button panel and add our button - with a custom css class.
		$('.ui-datepicker-buttonpane', thishtml).append(
			$('<button class="\
				ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all\
				"\>清除</button>'
			).click(function(){
				inst.input.attr('value', '会议日期');
				inst.input.datepicker('hide');
			})
		);
		
		thishtml = thishtml.children(); //remove the wrapper div
		
		return thishtml; //assume okay to return a jQuery
	};
	
	$('#span_login').bind('click', openLogin);
	$('#span_login_close').bind('click', closeLogin);
	$('#btn_login_cancel').bind('click', closeLogin);
	$('#btn_login').bind('click', login);
	$('#img_captcha').bind('click', changeCaptcha);
}

function changeCaptcha() {
	$('#img_captcha').attr('src', $('#captchaUrl').val() + '?r=' + Math.random());
}

/**
 * pop up login dialog.
 */
function openLogin() {
	//clear input field
	$('#login_form input[type="text"]').val('');
	$('#login_form input[type="password"]').val('');
	changeCaptcha();
	$('#span_login_msg').html('');
	$('#div_overlay').show();//width: 100%; height: 100%;
	var top = document.body.scrollTop;
	var left = document.body.scrollLeft;
	var height = document.body.clientHeight;
	var width = document.body.clientWidth;

	if (top==0 && left==0 && height==0 && width==0) {
		top = document.documentElement.scrollTop;
		left = document.documentElement.scrollLeft;
		height = document.documentElement.clientHeight;
		width = document.documentElement.clientWidth;
	}
	$('#div_overlay').css('top', top);
	$('#div_overlay').css('left', left);
	$('#div_overlay').css('width', width);
	$('#div_overlay').css('height', height);
	$('#div_login').show();
	$('#div_login').position({
		of : $('#wrapper'),
		offset : '0 -300'
	});
	
	$('#input_username').focus();
}

/**
 * close login dialog.
 */
function closeLogin() {
	$('#div_overlay').hide();
	$('#div_login').hide();
}

/**
 * login
 */
function login() {
	var loginForm = $('#login_form');
	$.ajax({
		  type: 'POST',
		  url: loginForm.attr('action'),
		  data: loginForm.serialize(),
		  success: afterLogin,
		  dataType: 'json'
		});
}

function afterLogin(result) {
	if (result == '1') {
		$('#span_login').html('&nbsp;' + $('#input_username').val());
		$('#span_login').unbind();
		closeLogin();
	} else if (result == '-2') {
		$('#login_form input[type="password"]').val('');
		$('#login_form input[name="captcha"]').val('');
		$('#span_login_msg').html('验证码错误');
		changeCaptcha();
	} else {
		$('#login_form input[type="password"]').val('');
		$('#login_form input[name="captcha"]').val('');
		$('#span_login_msg').html('用户名或密码错误');
		changeCaptcha();
	}
}

function searchCityFocusin() {
	if ($.trim($('#search_city').val()) == '城市') {
		$('#search_city').val('');
	}
}

function searchDateFocusin() {
	if ($.trim($('#search_date').val()) == '会议日期') {
		$('#search_date').val('');
	}
}

function searchIndFocusin() {
	if ($.trim($('#search_ind').val()) == '行业') {
		$('#search_ind').val('');
	}
}

function searchCityFocusout() {
	if ($.trim($('#search_city').val()) == '') {
		$('#search_city').val('城市');
	}
}

function searchDateFocusout() {
	if ($.trim($('#search_date').val()) == '') {
		$('#search_date').val('会议日期');
	}
	
}

function searchIndFocusout() {
	if ($.trim($('#search_ind').val()) == '') {
		$('#search_ind').val('行业');
	}

}

function searchBtnOnclick() {
	
	if ($.trim($('#search_city').val()) == '城市') {
		$('#hid_search_city').val('');
	} else {
		$('#hid_search_city').val($.trim($('#search_city').val()));
	}
	
	if ($.trim($('#search_date').val()) == '会议日期') {
		$('#hid_search_date').val('');
	} else {
		$('#hid_search_date').val($.trim($('#search_date').val()));
	}
	
	if ($.trim($('#search_ind').val()) == '行业') {
		$('#hid_search_ind').val('');
	} else {
		$('#hid_search_ind').val($.trim($('#search_ind').val()));
	}
	
	var searchForm = $('#search_form');
	freshCitiesTab($('#hid_search_city').val());
	freshIndustriesTab($('#hid_search_ind').val());
	$.ajax({
		  type: 'POST',
		  url: searchForm.attr('action'),
		  data: searchForm.serialize(),
		  success: freshRec,
		  dataType: 'json'
		});
}

function freshIndustriesTab(industryId) {
	$('#industries li').each(function(index){
		if (index != 0)
			$(this).remove();
	});
	if (industryId == '') {
		//Select All
		displayIndustriesTab(industries);
	} else {
		var doSelect = false;
		for (var i = 0; i < industries.length; i++) {
			if (industryId == industries[i].industryId) {
				selectIndIndex = i;
				doSelect = true;
				break;
			}
		}
		
		if (!doSelect) {
			//Select all because no industry was selected.
			displayIndustriesTab(industries);
		} else {
			if (selectIndIndex < maxCountOfInd) {
				//Industry selected is showing.
				var ul = $('#industries');
				var li = $(document.createElement('li'));
				li.addClass('toptab_all font_lb_white');
				li.html('全部');
				li.bind('click', {industryId : ''}, indTabOnclick);
				ul.append(li);
				for (var i = 0; i < (industries.length > maxCountOfInd ? maxCountOfInd : industries.length); i++) {
					appendIndustry(industries[i], i == selectIndIndex);
				}
				if (industries.length > maxCountOfInd) {
					li = $(document.createElement('li'));
					li.addClass('toptab_more font_lb_white');
					li.html('更多');
					li.attr('id', 'li_ind_more');
					li.hover(popupMoreInd, hideMoreInd);
					ul.append(li);
					
					var ul_ind_more_pop = $('#ul_ind_more_pop');
					$('#ul_ind_more_pop li').remove();
					for (var i = maxCountOfInd; i < industries.length; i++ ) {
						var popli = $(document.createElement('li'));
						var popspan = $(document.createElement('span'));
						popspan.addClass('font_lb_black more_pop');
						popspan.html(industries[i].name.substring(0, 2));
						popspan.attr('title', industries[i].name);
						popli.append(popspan);
						popli.bind('click', {industryId : industries[i].industryId}, indTabOnclick);
						ul_ind_more_pop.append(popli);
					}
				}
			} else {
				//Industry selected is in more tab.
				var ul = $('#industries');
				var li = $(document.createElement('li'));
				li.addClass('toptab_all font_lb_white');
				li.html('全部');
				li.bind('click', {industryId : ''}, indTabOnclick);
				ul.append(li);
				
				li = $(document.createElement('li'));
				li.addClass('toptab_s font_lb_yellow');
				//if (industries[selectIndIndex].name.length > 2) {
					var span = $(document.createElement('span'));
					span.html(industries[selectIndIndex].name.substring(0, 2));
					span.attr('title', industries[selectIndIndex].name);
					li.append(span);
				//} else {
				//	li.html(industries[selectIndIndex].name);
				//}
				ul.append(li);
				
				for (var i = 1; i < (industries.length > maxCountOfInd ? maxCountOfInd : industries.length); i++) {
					appendIndustry(industries[i - 1], false);
				}
				if (industries.length > maxCountOfInd) {
					li = $(document.createElement('li'));
					li.addClass('toptab_more font_lb_white');
					li.html('更多');
					li.attr('id', 'li_ind_more');
					li.hover(popupMoreInd, hideMoreInd);
					ul.append(li);
					
					var ul_ind_more_pop = $('#ul_ind_more_pop');
					$('#ul_ind_more_pop li').remove();
					for (var i = maxCountOfInd - 1; i < industries.length; i++ ) {
						if (selectIndIndex != i) {
							var popli = $(document.createElement('li'));
							var popspan = $(document.createElement('span'));
							popspan.addClass('font_lb_black more_pop');
							popspan.html(industries[i].name.substring(0, 2));
							popspan.attr('title', industries[i].name);
							popli.append(popspan);
							popli.bind('click', {industryId : industries[i].industryId}, indTabOnclick);
						}
						
						ul_ind_more_pop.append(popli);
					}
				}
			}
		}
	}
}

function freshCitiesTab(cityId) {
	$('#cities li').remove();
	if (cityId == '') {
		//Select All
		displayCitiesTab(cities);
	} else {
		var doSelect = false;
		for (var i = 0; i < cities.length; i++) {
			if (cityId == cities[i].cityId) {
				selectCityIndex = i;
				doSelect = true;
				break;
			}
		}
		
		if (!doSelect) {
			//Select all because no city was selected.
			displayCitiesTab(cities);
		} else {
			if (selectCityIndex < maxCountOfCity) {
				//City selected is showing.
				var ul = $('#cities');
				var li = $(document.createElement('li'));
				li.addClass('rtab_all font_lb_white');
				li.html('<br/>全部');
				li.bind('click', {cityId : ''}, cityTabOnclick);
				ul.append(li);
				for (var i = 0; i < (cities.length > maxCountOfCity ? maxCountOfCity : cities.length); i++) {
					appendCity(cities[i], i == selectCityIndex);
				}
				if (cities.length > maxCountOfCity) {
					li = $(document.createElement('li'));
					li.addClass('rtab_more font_lb_white');
					li.html('<br/>更多');
					li.attr('id', 'li_city_more');
					li.hover(popupMoreCities, hideMoreCities);
					ul.append(li);
					
					var ul_city_more_pop = $('#ul_city_more_pop');
					$('#ul_city_more_pop li').remove();
					for (var i = maxCountOfCity; i < cities.length; i++ ) {
						var popli = $(document.createElement('li'));
						var popspan = $(document.createElement('span'));
						popspan.addClass('font_lb_black more_pop');
						popspan.html(cities[i].name.substring(0, 2));
						popspan.attr('title', cities[i].name);
						popli.append(popspan);
						popli.bind('click', {cityId : cities[i].cityId}, cityTabOnclick);
						ul_city_more_pop.append(popli);
					}
				}
			} else {
				//City selected is in more tab.
				var ul = $('#cities');
				var li = $(document.createElement('li'));
				li.addClass('rtab_all font_lb_white');
				li.html('<br/>全部');
				li.bind('click', {cityId : ''}, cityTabOnclick);
				ul.append(li);
				
				li = $(document.createElement('li'));
				li.addClass('rtab font_lb_yellow');
				//if (cities[selectCityIndex].name.length > 3) {
					var span = $(document.createElement('span'));
					span.html('<br/>' + cities[selectCityIndex].name.substring(0, 2));
					span.attr('title', cities[selectCityIndex].name);
					li.append(span);
				//} else {
				//	li.html('<br/>' + cities[selectCityIndex].name);
				//}
				ul.append(li);
				
				for (var i = 1; i < (cities.length > maxCountOfCity ? maxCountOfCity : cities.length); i++) {
					appendCity(cities[i - 1], false);
				}
				if (cities.length > maxCountOfCity) {
					li = $(document.createElement('li'));
					li.addClass('rtab_more font_lb_white');
					li.html('<br/>更多');
					li.attr('id', 'li_city_more');
					li.hover(popupMoreCities, hideMoreCities);
					ul.append(li);
					
					var ul_city_more_pop = $('#ul_city_more_pop');
					$('#ul_city_more_pop li').remove();
					for (var i = maxCountOfCity - 1; i < cities.length; i++ ) {
						if (selectCityIndex != i) {
							var popli = $(document.createElement('li'));
							var popspan = $(document.createElement('span'));
							popspan.addClass('font_lb_black more_pop');
							popspan.html(cities[i].name.substring(0, 2));
							popspan.attr('title', cities[i].name);
							popli.append(popspan);
							popli.bind('click', {cityId : cities[i].cityId}, cityTabOnclick);
							ul_city_more_pop.append(popli);
						}
					}
				}
			}
		}
	}
}

/**
 * click event on industry tab, filter the recommand meeting list.
 * @param event
 */
function indTabOnclick(event) {
	hideMoreInd();
	$('#hid_search_ind').val(event.data.industryId);
	var searchForm = $('#search_form');
	freshCitiesTab($('#hid_search_city').val());
	freshIndustriesTab($('#hid_search_ind').val());
	$.ajax({
		  type: 'POST',
		  url: searchForm.attr('action'),
		  data: searchForm.serialize(),
		  success: freshRec,
		  dataType: 'json'
		});
}

/**
 * click event on industry tab, filter the recommand meeting list.
 * @param event
 */
function cityTabOnclick(event) {
	$('#hid_search_city').val(event.data.cityId);
	var searchForm = $('#search_form');
	freshCitiesTab($('#hid_search_city').val());
	freshIndustriesTab($('#hid_search_ind').val());
	$.ajax({
		  type: 'POST',
		  url: searchForm.attr('action'),
		  data: searchForm.serialize(),
		  success: freshRec,
		  dataType: 'json'
		});
}

/**
 * click event on meeting title, display the meeting detail.
 * @param event
 */
function getMeetingDetail(event) {
	$.ajax({
		  type: 'POST',
		  url: $('#detailUrl').val(),
		  data: 'meetingQueryParasVO.meetingId_e=' + event.data.meetingId,
		  success: displayMeetingDetail,
		  dataType: 'json'
		});
}

/**
 * display the meeting detail.
 */
function displayMeetingDetail(meeting) {
	var nav_title = $('#li_nav');
	nav_title.html('会议概况');
	
	//remove the li but nav_title
	$("#industries li").each(function(index) {
		if (index > 0)
			$(this).remove();
	});
	var divRight = $('#div_right');
	divRight.children().remove();
	var divTitle = $(document.createElement('div'));
	divTitle.addClass('detail_t');
	var spanTitle = $(document.createElement('span'));
	spanTitle.addClass('detail_t');
	spanTitle.html(meeting.name);
	divTitle.append(spanTitle);
	divRight.append(divTitle);
	
	var divDate = $(document.createElement('div'));
	divDate.addClass('detail_c');
	var spanDate = $(document.createElement('span'));
	spanDate.addClass('detail_c');
	spanDate.html('【会议时间】&nbsp;' + meeting.startDateTimeStr + ' 至 ' + meeting.endDateTimeStr);
	divDate.append(spanDate);
	divRight.append(divDate);
	
	var divOrg = $(document.createElement('div'));
	divOrg.addClass('detail_c');
	var spanOrg = $(document.createElement('span'));
	spanOrg.addClass('detail_c');
	spanOrg.html('【主办单位】&nbsp;' + meeting.organizer);
	divOrg.append(spanOrg);
	divRight.append(divOrg);
	
	var divLocation = $(document.createElement('div'));
	divLocation.addClass('detail_c');
	var spanLocation = $(document.createElement('span'));
	spanLocation.addClass('detail_c');
	spanLocation.html('【地点】&nbsp;' + meeting.location);
	divLocation.append(spanLocation);
	divRight.append(divLocation);
	
	var divDescTitle = $(document.createElement('div'));
	divDescTitle.addClass('detail_c');
	var spanDescTitle = $(document.createElement('span'));
	spanDescTitle.addClass('detail_c');
	spanDescTitle.html('【展会介绍】');
	divDescTitle.append(spanDescTitle);
	divRight.append(divDescTitle);
	
	var divDesc = $(document.createElement('div'));
	divDesc.addClass('detail_c');
	var spanDesc = $(document.createElement('span'));
	spanDesc.addClass('detail_info');
	spanDesc.html(meeting.description);
	divDesc.append(spanDesc);
	divRight.append(divDesc);
	
	$('#cities li').remove();
	
	var ulRtab = $('#cities');
	var liMetGener = $(document.createElement('li'));
	liMetGener.addClass('rtab_l_s font_lb_yellow');
	liMetGener.html('<br/>会议概况');
	ulRtab.append(liMetGener);
	
	var liMetSched = $(document.createElement('li'));
	liMetSched.addClass('rtab_l font_lb_white');
	liMetSched.html('<br/>会议日程');
	ulRtab.append(liMetSched);
	
	var liMetRelev = $(document.createElement('li'));
	liMetRelev.addClass('rtab_l font_lb_white');
	liMetRelev.html('<br/>参会相关');
	ulRtab.append(liMetRelev);
	
	var liMetServ = $(document.createElement('li'));
	liMetServ.addClass('rtab_l font_lb_white');
	liMetServ.html('<br/>会场服务');
	ulRtab.append(liMetServ);
	
	var liMetLive = $(document.createElement('li'));
	liMetLive.addClass('rtab_l_17 font_lb_white');
	liMetLive.html('<br/>会议直播间');
	ulRtab.append(liMetLive);
	
	var liMetDisc = $(document.createElement('li'));
	liMetDisc.addClass('rtab_l_17 font_lb_white');
	liMetDisc.html('<br/>会议直播间');
	ulRtab.append(liMetDisc);
}